<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/8/22
 * Time: 11:39
 */

namespace app\service;


use Redis\Redis;
use think\Db;

class IntegralService
{
    static $_redis;
    function __construct()
    {
        self::$_redis=new Redis();
    }

    function integralShop($pro_type="",$min="",$max="",$order='asc')
    {
        $where="pro_status=1 and putaway_time<='".date("Y-m-d H:i:s")."' and under_time>'".date("Y-m-d H:i:s")."' and plurs_num>0";
        if(!empty($pro_type)){
            $where.=" and pro_type={$pro_type} ";
        }
        if(!empty($min)){
            $where.=" and integral_num>={$min}";
        }
        if(!empty($max)){
            $where.=" and integral_num<{$max}";
        }
        $list=Db::table("integral_exchange")
            ->where($where)
            ->order("integral_num {$order}")
            ->select();
        if(!empty($list)){
            foreach($list as $k=>$v){
                if(!empty($v['bannerurl'])){
                    $list[$k]['bannerArr']=explode(',',$v['bannerurl']);
                }
                $is_set=self::$_redis->hGetJson("integralShop",strval($v['id']));
                if(empty($is_set)){
                    # 计算下架时间还有多少秒
                    $_time=strtotime($v['under_time'])-time();
                    self::$_redis->hSetJson("integralShop",strval($v['id']),$list[$k],$_time);
                }
            }
            return array("status"=>true,"data"=>$list);
        }else{
            return array("status"=>false);
        }
    }

    function immediatelyChange($id,$member_code)
    {
       $item=self::$_redis->hGetJson("integralShop",strval($id));
        if(empty($item)){
            return array("status"=>false,"msg"=>"系统错误");
        }
        # 判断该商品是否已经下架
        if($item['under_time']<=date("Y-m-d H:i:s") or $item['pro_status']!==1){
            return array("status"=>false,"msg"=>"该商品已下架");
        }
       # 判断商品数量是否还有
        if($item['plurs_num']<1){
            return array("status"=>false,"msg"=>"商品已售完");
        }
        $memberInfo=self::$_redis->hGetJson("memberInfo",$member_code);
       # 判断用户积分是否足够
        if(floatval($item['integral_num'])>floatval($memberInfo['integral'])){
            return array("status"=>false,"msg"=>"积分不足");
        }
        # 判断是否限购
        if($item['limit_num']>0){
            $member_log=Db::table("log_integral")->where(array("member_code"=>$member_code,"status"=>2,"type"=>2,"order_id"=>$id))->count();
            if($item['limit_num']<$member_log){
                return array("status"=>false,"msg"=>"已超出商品限购数量");
            }
        }
        # 减少剩余数量
        $item['plurs_num']-=1;
        self::$_redis->hSetJson("integralShop",strval($id),$item);
        Db::table("integral_exchange")->where(array("id"=>$id))->setDec("plurs_num",1);
        # 兑换给用户加对应商品 1.金币 2 恢复材料 3 卡券
        if($item['pro_type']==1){
            # 增加金币
            $res=Db::table("member_property")->where(array("member_code"=>$member_code))->setInc("gold",$item['pro_number']);
            $memberInfo['gold']= priceFormat($memberInfo['gold']+$item['pro_number']);
            # 增加金币记录
            MemberService::addLog_gold($member_code,$item['pro_number'],18);
        }elseif($item['pro_type']==2){
            # 增加恢复材料
            $res=Db::table("member_property")->where(array("member_code"=>$member_code))->setInc("materials",$item['pro_number']);
            # 恢复材料记录
            $memberInfo['materials']= priceFormat($memberInfo['materials'] + $item['pro_number']);
            MemberService::addLog_materials($member_code,$item['pro_number'],4,1);
        }else{
            # 查询卡券信息
            $voucher=Db::table("voucher")->where(array("id"=>$item['pro_number']))->find();
            # 添加卡券
            $res= Db::table("member_voucher")->insert(array("member_code"=>$member_code,"voucher_title"=>$voucher['title'],
                "voucher_id"=>$item['pro_number'],"price"=>$voucher['price'],"expiration_time"=>date("Y-m-d H:i:s",strtotime("+".$item['period'])),
                "get_time"=>date("Y-m-d H:i:s"),"source"=>1,"source_branch"=>2,"voucher_perioy"=>$item['period']));
        }
        if($res){
            # 扣除积分
            $memberInfo['integral']= priceFormat($memberInfo['integral']-$item['integral_num']);
            Db::table("member_property")->where(array("member_code"=>$member_code))->setDec("integral",$item['integral_num']);
            self::$_redis->hSetJson("memberInfo",$member_code,$memberInfo);
            # 加积分记录
            MemberService::addLog_integral($member_code,$item['integral_num'],2,2);
            return array("status"=>true);
        }else{
            return array("status"=>false,"msg"=>"兑换失败");
        }


    }
}